package com.marvels.approve.manager.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.marvels.approve.manager.dao.ICaseNodeRoleDao;
import com.marvels.approve.manager.dto.CaseNodeRoleDTO;
import com.marvels.approve.manager.service.ICaseNodeRoleService;
import com.marvels.common.exception.MarvelsBusinessException;
/**流程角色服务
 * @author 何杰
 * 2018年4月10日
 */
@Service
public class CaseNodeRoleServiceImpl implements ICaseNodeRoleService {
	@Autowired
	private ICaseNodeRoleDao caseNodeRoleDao;
	@Override
	public List<CaseNodeRoleDTO> findCaseNodeRoles(CaseNodeRoleDTO caseNodeRoleDTO) throws MarvelsBusinessException {
		// TODO 自动生成的方法存根
		caseNodeRoleDTO = caseNodeRoleDTO == null?new CaseNodeRoleDTO():caseNodeRoleDTO;
		PageHelper.startPage(caseNodeRoleDTO.getPageNum(),caseNodeRoleDTO.getPageSize());
		return this.caseNodeRoleDao.select(caseNodeRoleDTO);
	}

	@Override
	public CaseNodeRoleDTO saveCaseNodeRole(CaseNodeRoleDTO caseNodeRole) throws MarvelsBusinessException {
		//新增
		if(caseNodeRole.getId()==null){
			add(caseNodeRole);
		}else{//更新
			update(caseNodeRole);
		}
		return caseNodeRole;
	}
	private CaseNodeRoleDTO add(CaseNodeRoleDTO caseNodeRole)throws MarvelsBusinessException {
		//检查重复
		CaseNodeRoleDTO dto = new CaseNodeRoleDTO();
		dto.setNodeCode(caseNodeRole.getNodeCode());
		dto.setRoleCode(caseNodeRole.getRoleCode());
		List datas = this.caseNodeRoleDao.select(dto);
		if(datas!=null && datas.size() !=0 ){
			throw new MarvelsBusinessException("流程节点["+caseNodeRole.getNodeName()+"]对应权限["+caseNodeRole.getRoleName()+"]已经存在!");
		}
		caseNodeRole.setCreatedBy(caseNodeRole.obtainLoginUserName());
		caseNodeRole.setUpdatedBy(caseNodeRole.obtainLoginUserName());
		this.caseNodeRoleDao.insert(caseNodeRole);
		return caseNodeRole;
	}
	private CaseNodeRoleDTO update(CaseNodeRoleDTO caseNodeRole)throws MarvelsBusinessException {
		CaseNodeRoleDTO db = this.caseNodeRoleDao.selectByPrimaryKey(caseNodeRole.getId());
		if(db==null){
			throw new MarvelsBusinessException("无相关设置，请确认是否正常!");
		}
		if(db.getStatus()==caseNodeRole.getStatus()){
			throw new MarvelsBusinessException("状态未变更，无需更新");
		}
		caseNodeRole.setUpdatedBy(caseNodeRole.obtainLoginUserName());
		this.caseNodeRoleDao.updateByPrimaryKeySelective(caseNodeRole);
		return caseNodeRole;
	}

	@Override
	public CaseNodeRoleDTO findCaseNodeRole(Long id) throws MarvelsBusinessException {
		// TODO 自动生成的方法存根
		return this.caseNodeRoleDao.selectByPrimaryKey(id);
	}

	@Override
	public List<CaseNodeRoleDTO> findCaseNodeRoles(List<String> roles) throws MarvelsBusinessException {
		if(roles==null || roles.size() <1){
			throw new MarvelsBusinessException("通过角色查询节点,角色不能为空！");
		}
		List<CaseNodeRoleDTO> dtos = this.caseNodeRoleDao.selectNodesByRoles(roles);
		if(dtos==null || dtos.size() <1){
			throw new MarvelsBusinessException("通过角色查询节点,没有可用节点可用！");
		}
		return dtos;
	}

}
